

<body>

    <div class="hd">
        <div class="warp">
            <ul class="btn-list">
                <li class="item">
                    <a href="<?php echo $base_url?>home/order/shoppingcar" class="shop-car bg">
                        <!--<div class="num">1</div>-->
                        <i class="icon iconfont icon-icon"></i>
                    </a>
                </li>
                 <li  class="item">
<!--                     <a href="--><?php //echo $base_url; ?><!--home/account/login">-->
<!--                        <i class="iconfont icon-wode" ></i>请登录-->
<!--                    </a>-->
                     <?php if(isset($user)){?>
                         <span href="#" class="bg">
                        <i class="icon iconfont icon-wode"></i><?php echo $user->mobile;?>
                    </span>

                         <ul class="child-item">
                             <li><a href="<?php echo $base_url?>home/order/userorders">我的订单</a></li>
                             <li><a href="<?php echo $base_url?>home/address">收货地址</a></li>
                             <li><a href="<?php echo $base_url;?><?php if(!empty($cardid)){echo 'home/card/cardlist';}else{echo 'home/card/addcard';}?>">绑定银行卡</a></li>
                             <li><a href="<?php echo $base_url?>home/account/logout">退出商城</a></li>

                         </ul>
                     <?php }else{?>
                         <a href="<?php echo $base_url?>home/account/login?back_url=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>">
                             <i class="iconfont icon-wode"></i>请登录
                         </a>
                     <?php }?>

                </li>
            </ul>
            <a href="./">
            <img src="<?php echo $base_url?>static/dist/images/logo.png" alt="" class="logo-img">
             <span class="span_txt">商城首页</span>
            </a>

        </div>
    </div>

    <div class="address_new_bar">
        <div class="warp">
             <p class="tit">
                新增收货地址
            </p>
            <div class="cont">
                <div class="input_txt2" id="location">
                    <span>所在地区</span>
                    <div class="picker-warp">
                        <!-- container -->
                        <input id="city-picker1" readonly type="text">
                        <!--<input id="city-picker1" readonly type="text" data-value="2/52/502">-->

                    </div>
                </div>
                <div class="input_txt">
                    <span>详细地址</span>
                    <input type="text" placeholder="请填写详细地址" id="address" maxlength="30">
                </div>
                <div class="input_txt" >
                    <span>收货人</span>
                    <input type="text" id="consignee" maxlength="15" min="2">
                </div>
                <div class="input_txt phone" >
                    <span>联系电话</span>
                    <div class="qh">0086</div>
                    <span class="line">一</span>
                    <input type="text" id="mobile" maxlength="11">
                </div>

                <div class="pd50"></div>
                <a href="#123" class="save-btn">保存</a>
            </div>
        </div>

    </div>
    <div class="address-bar">
        <div class="warp">
            <p class="tit">
                已保存<span><?php echo sizeof($content);?></span>条地址
            </p>
            <ul>
                <?php foreach ($content as $value) : ?>
                    <li class="address-item <?php echo $value['default'] ? 'active' : '' ?>" data-addressid="<?php echo $value['address_id']?>" data-value="<?php echo $value['province'];?>/<?php echo $value['city'];?>/<?php echo $value['district'];?>" data-text="<?php echo $value['province_text'];?>/<?php echo $value['city_text'];?>/<?php echo $value['district_text'];?>">
                        <p class="name">
                            <?php echo $value['consignee']; ?>
                        </p>
                        <p class="phone"><?php echo $value['mobile']; ?></p>

                        <p class="address_txt" data-provinceid="<?php echo $value['province'];?>" data-cityid="<?php echo $value['city'];?>" data-districtid="<?php echo $value['district'];?>"><?php echo $value['province_text'] . $value['city_text'] . $value['district_text'] . $value['address']; ?></p>
                        <div class="address_ft">
                            <a href="#123" class="r del_btn" data-addressid="<?php echo $value['address_id']?>">删除</a>
                            <span class="r line">|</span>
                            <a href="#123" class="r edit_btn" data-addressid="<?php echo $value['address_id']?>">修改</a>
                            <a class="default" data="<?php echo $value['address_id']?>"><?php echo $value['default'] ? "默认地址" : "设置为默认地址";?></a>
                        </div>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>





    <div class="pd-200"></div>

    <?php $this->load->view('home/pc/comm'); ?>

    <div class="mask-box none">
    </div>

    // 修改收货地址
    <div class="add_address" id="edit_address">

        <div class="bg">
            <a class="col_btn" id="close_edit_address">
                <i class="iconfont icon-icon1"></i>
            </a>
            <p class="tit">修改收货地址</p>
            <div class="cont">
                <div class="input_txt2">
                    <span>所在地区</span>
                    <div class="picker-warp">
                        <!-- container -->
                        <input id="city-picker2" readonly type="text">

                        <input type="hidden" id="address_ids" value="">
<!--                        <input id="city-picker2" readonly type="text" value="2/52/502">-->
                    </div>
                </div>
                <div class="input_txt">
                    <span>详细地址</span>
                    <input type="text" placeholder="请填写详细地址" id="edit_address2" maxlength="30" min="5">
                </div>
                <div class="input_txt">
                    <span>收货人</span>
                    <input type="text" id="edit_username" maxlength="15" min="2">
                </div>
                <div class="input_txt phone">
                    <span>联系电话</span>
                    <div class="qh">0086</div>
                    <span class="line">一</span>
                    <input type="text" id="edit_mobile" maxlength="11">
                </div>

                <div class="pd50"></div>
                <a href="#123" class="save-btn">保存</a>
            </div>
        </div>
    </div>


    <script src="<?php echo $base_url?>static/dist/js/lib/jquery.min.js"></script>
    <script src="<?php echo $base_url?>static/dist/js/lib/city-picker.js"></script>
    <script>


        // 修改收货地址时  数据填充
        $('.edit_btn').on('click',function(){
            var address_id = $(this).attr('data-addressid');

            $.ajax({
                url: "<?php echo $base_url; ?>home/address/getAddressByAid?"+"address_id="+address_id,
                dataType: 'json',
                type: 'POST',
                success: function (data) {                    
                    if (data.result == 0) {
//                       console.log(data.errmsg);
                        $('#edit_address2').val(data.errmsg.address);
                        $('#edit_mobile').val(data.errmsg.mobile);
                        $('#edit_username').val(data.errmsg.consignee);
                        $('#address_ids').val(data.errmsg.address_id);

                    } else {
                        alert(data.errmsg);
                    }
                }
            });

        });

        // 修改收货地址时  数据保存
        $("#edit_address").on('click','.save-btn',function(){
           var selArr = $("#edit_address").find(".city-picker-span .title").find(".select-item");
           var selValArr = [];
           selArr.each(function(i,el){
               selValArr.push($(el).data('code'));
           })

            var address = $("#edit_address2").val().trim();
            var consignee = $("#edit_username").val().trim();
            var mobile = $("#edit_mobile").val().trim();
            var address_id = $('#address_ids').val();

            var provinceid = selValArr[0];
            var cityid = selValArr[1];
            var districtid =selValArr[2];

            if( consignee == '' || mobile == '' || address == '' ||
                provinceid == undefined || cityid == undefined)
            {
                alert('请补全收货地址信息');
                return;
            }
            var pattern = /^[0-9]*$/;
            if(!pattern.test(mobile)){
                alert("请填写正确的数字号码");
                return;
            }
            if(consignee.length<2 || consignee.length>15)
            {
                alert('收货人填写错误');
                return;
            }
            if(address.length < 5 || address.length>30)
            {
                alert('详细地址输入不完整或太长');
                return;
            }
            if (districtid == undefined)
            {
                districtid = 0;
            }
            $.ajax({
                url: "<?php echo $base_url; ?>home/address/updateAddress",
                data: {
                    'address_id': address_id,
                    'consignee': consignee,
                    'mobile': mobile,
                    'provinceid': provinceid,
                    'cityid': cityid,
                    'districtid': districtid,
                    'address': address
                },
                dataType: 'json',
                type: 'POST',
                success: function (data) {
                    if (data.result == 0) {
                        location.reload();
                    } else {
                        alert(data.errmsg);
                    }
                }
            });

        })



        $('.col_btn').on('click', function () {
            $('.mask-box').addClass('none');
            $(this).parents('.add_address').removeClass('active');
        });

        var api = {
            province_url: '<?php echo $base_url?>home/address/getZonesByID?zoneid=1',
            other_url: '<?php echo $base_url?>home/address/getZonesByParentID',
        }
        var dataPicker = {
            PROVINCE_DATA: null,
            CITY_DATA: null,
            DISTRICT_DATA: null
        }
        var $citypicker1,$citypicker2;
        getProvinceData(function (data) {
            provinceData = formatProvince(data);
            dataPicker.PROVINCE_DATA = provinceData;
            $citypicker1 = $('#city-picker1');
            $citypicker1.citypicker({
                spanClick: spanClickFn,
                data: dataPicker
            });
            $citypicker2 = $('#city-picker2');
            $citypicker2.citypicker({
                spanClick: spanClickFn,
                data: dataPicker
            });


            $('body').on('click', '.edit_btn', function () {

                var $this = $(this);
                var valStr = $this.parents(".address-item").data("value");
                var txtStr = $this.parents(".address-item").data("text");
                var valArr = formatDataStr(valStr)
                var txtArr = formatDataStr(txtStr)

                if (valArr[0]) {
                    getData(valArr[0], true, dataPicker, function () {
                        if (valArr[1]) {
                            getData(valArr[1], false, dataPicker, function () {
                                if($citypicker2){
                                    $citypicker2.citypicker("reset");
                                    $('#edit_address').find(".picker-warp").find('.city-picker-span').remove();
                                    $('#edit_address').find(".picker-warp").find('.city-picker-dropdown').remove();
                                }

                                $citypicker2 = $('#city-picker2');
                                $citypicker2.citypicker({
                                    spanClick: spanClickFn,
                                    data: dataPicker,
                                    everyCreate:true, //每次都创建
                                    province: txtArr[0] || "",
                                    city: txtArr[1] || "",
                                    district: txtArr[2] || "",
                                })
                                $('#edit_address').addClass('active');
                                $('.mask-box').removeClass('none');

                            });
                        }
                    });
                }


            })
        })


        function spanClickFn(el, cb) {
            var code = $(el).data('code');
            var province = false;
            var city = false;
            if ($(el).parents(".province").length != 0) {
                province = true;
            } else if ($(el).parents(".city").length != 0) {
                city = true;
            } else if ($(el).parents(".district").length != 0) {
                cb();
                return true;
            }
            $.ajax({
                'url': api.other_url + "?zoneid=" + code,
                type: 'get',
                success: function (data) {
                    data = JSON.parse(data);
                    if (province) {
                        dataPicker.CITY_DATA = formatOtherData(data);
                        dataPicker.DISTRICT_DATA = null;
                    } else {
                        dataPicker.DISTRICT_DATA = formatOtherData(data);
                    }
                    // $this.refresh();
                    cb();
                }
            });
        }

        function getData(code, province, dataPicker, cb) {
            $.ajax({
                'url': api.other_url + "?zoneid=" + code,
                type: 'get',
                success: function (data) {
                    data = JSON.parse(data);
                    if (province) {

                        dataPicker.CITY_DATA = formatOtherData(data);
                    } else {
                        dataPicker.DISTRICT_DATA = formatOtherData(data);

                    }
                    // $this.refresh();
                    cb();
                }
            })
        }

        function formatDataStr(str) {
            var arr = [];
            if (!str) return [];
            return str.split('/');
        }

        function formatProvince(data) {
            var newArr = [], newObj = {};
            for (var i = 0; i < data.length; i++) {
                // newObj[data[i].id] =  data[i].name;
                var dataTmp = {}
                dataTmp.code = data[i].id;
                dataTmp.address = data[i].name;
                newArr.push(dataTmp);
            }
            newObj[" "] = newArr
            return newObj;

        }
        function getProvinceData(cb) {
            $.ajax({
                'url': api.province_url,
                type: 'get',
                success: function (data) {
                    data = JSON.parse(data);
                    cb(data);
                },
                error: function () {
                    console.log('Ajax Error!');
                }

            });
        }
        function formatOtherData(data) {
            var newObj = {};
            if (data == null) {
                return newObj;
            }
            for (var i = 0; i < data.length; i++) {
                // newObj[data[i].id] =  data[i].name;
                newObj[data[i].id] = data[i].name
            }
            return newObj;
        }
        function isEmptyObject(obj) {
            for (var key in obj) {
                return false;
            }
            return true;
        }

        //设置默认地址
        $(".default").on('click',function(){
            var id = $(this).attr('data');

            $.ajax({
                url: "<?php echo $base_url; ?>home/Users/addDefaultUserAddress",
                data: {
                    address_id: id
                },
                dataType: 'json',
                type: 'POST',
                success: function (data) {
                    console.log(data);
                    if (data.result == 0) {
                        location.href = "<?php echo $base_url; ?>home/address<?php if(isset($dataFromGet)) echo '?data=' . urlencode($dataFromGet);?>";
                    } else {
                        alert(data.errmsg);
                    }
                }
            });
        })

        $(function(){
            $('#close_edit_address').on('click', function(){
                $('#edit_address').css('display', 'none');
            });

            //点击修改
            $('.address-bar').on('click', 'li .edit_btn', function(){
                $('#edit_address').css('display', 'block');
            });

            //保存新增收货地址
            $('.address_new_bar').on('click', '.save-btn', function(){
                var consignee = $('.address_new_bar #consignee').val().trim();
                var mobile = $('.address_new_bar #mobile').val().trim();
                var address = $('.address_new_bar #address').val().trim();
                var provinceid = $('.address_new_bar #location .title [data-count="province"]').data('code');
                var cityid = $('.address_new_bar #location .title [data-count="city"]').data('code');
                var districtid = $('.address_new_bar #location .title [data-count="district"]').data('code');
                if( consignee == '' || mobile == '' || address == '' ||
                    provinceid == undefined || cityid == undefined )
                {
                    alert('请补全收货地址信息');
                    return;
                }
                var pattern = /^[0-9]*$/;
                if(!pattern.test(mobile)){
                    alert("请填写正确的数字号码");
                    return;
                }
                if(address.length < 5)
                {
                    alert('详细地址输入不完整');
                    return;
                }
                if (districtid == undefined)
                {
                    districtid = 0;
                }
                $.ajax({
                    url: "<?php echo $base_url; ?>home/address/add",
                    data: {
                        'consignee': consignee,
                        'mobile': mobile,
                        'provinceid': provinceid,
                        'cityid': cityid,
                        'districtid': districtid,
                        'address': address
                    },
                    dataType: 'json',
                    type: 'POST',
                    success: function (data) {
                        if (data.result == 0) {
                            location.reload();
                        } else {
                            alert(data.errmsg);
                        }
                    }
                });
            });

            //删除收货地址
            $('.address-bar li').on('click', '.del_btn', function(){

                if (!confirm("您确定删除该地址吗？")) return;
                var address_id = $(this).data('addressid');
                $.ajax({
                    url: "<?php echo $base_url; ?>home/address/delAddressById?",
                    dataType: 'json',
                    type: 'POST',
                    data:{address_id:address_id},
                    success: function (data) {
                        console.log(data);
                        if (data.result == 0) {
                            location.reload();
                        } else {
                            alert(data.errmsg);
                        }
                    }
                });
            });


        });

    </script>
    <?php $this->load->view('home/pc/footer_js');?>
</body>

</html>
